package com.golden.procedure.service.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.golden.procedure.dao.WorkTaskHistoryMapper;
import com.golden.procedure.domain.WorkTaskHistoryDO;
import com.golden.procedure.entity.WorkTaskHistory;
import com.golden.procedure.result.ResultDO;
import com.golden.procedure.result.ResultSupport;
import com.golden.procedure.service.IWorkTaskHistoryService;
import com.golden.procedure.util.BeanUtilsExtends;
import com.golden.procedure.util.BonConstant;

@Service("workTaskHistoryService")
public class WorkTaskHistoryService extends BaseService implements IWorkTaskHistoryService{

	@Autowired
	WorkTaskHistoryMapper taskHistoryMapper;

	/**
	 * 添加工单任务记录 <b>Description: 添加工单任务记录</b><br>
	 * 
	 * @param domain
	 * @return ResultDO
	 * @Note <b>Author:</b> Reading.Reagan <br>
	 *       <b>Date:</b> 2018年9月17日 下午5:47:17 <br>
	 *       <b>Version:</b> 1.0
	 */
	@Override
	public ResultDO add(WorkTaskHistoryDO domain) {
		// TODO 安排等级
		ResultDO result = new ResultSupport();
		WorkTaskHistory entity = new WorkTaskHistory();
		if (!BeanUtilsExtends.copyProperties(entity, domain)) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("数据转换异常");
			return result;
		}
		if (StringUtils.isNotEmpty(domain.getSchedulingdateStr())) {
			entity.setSchedulingdate(new Date(Long.parseLong(domain.getSchedulingdateStr())));
		}
		if (StringUtils.isNotEmpty(domain.getPlanStartOnStr())) {
			entity.setPlanStartOn(new Date(Long.parseLong(domain.getPlanStartOnStr())));
		}
		if (StringUtils.isNotEmpty(domain.getPlanEndOnStr())) {
			entity.setPlanEndOn(new Date(Long.parseLong(domain.getPlanEndOnStr())));
		}
		if (StringUtils.isNotEmpty(domain.getStartOnStr())) {
			entity.setStartOn(new Date(Long.parseLong(domain.getStartOnStr())));
		}
		entity.setIsDelete(BonConstant.DB_NORMAL);

		int ret = taskHistoryMapper.insert(entity);
		if (ret < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("工单任务记录添加失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

}
